package com.letao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.letao.common.pojo.EUDataGridResult;
import com.letao.common.pojo.LetaoResult;
import com.letao.common.utils.HttpClientUtil;
import com.letao.mapper.TbContentMapper;
import com.letao.pojo.TbContent;
import com.letao.pojo.TbContentExample;
import com.letao.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by ac on 2017/1/18.
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    @Value("${REST_BASE_URL}")
    private String REST_BASE_URL;

    @Value("${REST_CONTENT_SYNC_URL}")
    private String REST_CONTENT_SYNC_URL;




    @Override
    public LetaoResult insertContent(TbContent content) {
        content.setCreated(new Date());
        content.setUpdated(new Date());
        tbContentMapper.insert(content);
        //调用缓存同步服务
        try {
            HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_SYNC_URL + content.getCategoryId());
        }catch (Exception e){
            e.printStackTrace();
        }

        return LetaoResult.ok();
    }

    @Override
    public EUDataGridResult getContentList(Long categoryID, Integer page, Integer rows) {
        TbContentExample tbContentExample = new TbContentExample();
        tbContentExample.createCriteria().andCategoryIdEqualTo(categoryID);
        PageHelper.startPage(page, rows);
        List<TbContent> list = tbContentMapper.selectByExample(tbContentExample);
        PageInfo<TbContent> pageInfo = new PageInfo<>(list);
        return new EUDataGridResult(pageInfo.getTotal(), list);
    }
}
